package org.test;

/**
 * 方位的计算
 * 两个坐标的方位。没有通过jar包
 */
public class D {
    /**
     * GIS方位角度，正北为0度，顺时针旋转
     *
     * @param lat1 坐标1纬度
     * @param lon1 坐标1经度
     * @param lat2 坐标2纬度
     * @param lon2 坐标2经度
     * @return 坐标1到坐标2方位角度
     */
    public static double bearing(double lat1, double lon1, double lat2, double lon2) {
        double longitude1 = lon1;
        double longitude2 = lon2;
        double latitude1 = Math.toRadians(lat1);
        double latitude2 = Math.toRadians(lat2);
        double longDiff = Math.toRadians(longitude2 - longitude1);
        double y = Math.sin(longDiff) * Math.cos(latitude2);
        double x = Math.cos(latitude1) * Math.sin(latitude2) - Math.sin(latitude1) * Math.cos(latitude2) * Math.cos(longDiff);
        return (Math.toDegrees(Math.atan2(y, x)) + 360) % 360;
    }

    public static void main(String[] args) {
        //坐标1为北京，坐标2为广州
        double bearing = D.bearing(39.923984, 116.534119, 23.140121, 113.29845);
        System.out.println(bearing);//190.2221319576134

        double bearing2 = D.bearing(PointeDef.beijing.getLatitude(), PointeDef.beijing.getLongitude(),
                PointeDef.guangzhou.getLatitude(), PointeDef.guangzhou.getLongitude());
        System.out.println(bearing2);
    }
}
